<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 500px;
            margin: 100px auto;
            border-collapse: collapse;
            text-align: center;
        }
        
        td,
        th {
            border: 1px solid #333;
        }
        
        thead tr {
            height: 40px;
            background-color: #ccc;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <tr>
                <th>姓名</th>
                <th>科目</th>
                <th>成绩</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <script>
        //1.首先准备学生数据
        var datas = [{
            name: '魏璎珞',
            subject: 'JavaScript',
            score: 100
        }, {
            name: '弘历',
            subject: 'JavaScript',
            score: 98
        }, {
            name: '傅恒',
            subject: 'JavaScript',
            score: 99
        }, {
            name: '明玉',
            subject: 'JavaScript',
            score: 88
        }];
        //往 tbody里面创建行 :有几个人就创建几行
        //1.获取tbody
        var tbody = document.querySelector('tbody');
        for (var i = 0; i < datas.length; i++) {
            //创建tr行
            var tr = document.createElement('tr');
            //插入到tbody中去
            tbody.appendChild(tr);
            //行里面创建单元格 td 单元格的数量取决于每个对象的属性个数
            for (var k in datas[i]) { //k是属性 obj是对象
                //创建单元格
                var td = document.createElement('td');

                //把对象里面的属性值给td
                td.innerHTML = datas[i][k];
                //创建好的单元格放到行里面
                tr.appendChild(td);

            }
            //3.创建有删除两个字的单元格
            var td = document.createElement('td');
            td.innerHTML = '<a href ="javascript:;">删除</a>';
            tr.appendChild(td);

        }
        //删除操作开始
        var as = document.querySelectorAll('a');
        for (var i = 0; i < as.length; i++) {
            as[i].onclick = function() {
                //点击a 删除当前链接所在的行(链接爸爸的爸爸) node.removeChild(child)
                tbody.removeChild(this.ParentNode.ParentNode)

            }
        }
    </script>
</body>

</html>